package ahblue.cn.tanzi.ui.person;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.model.user.CodeModel;
import ahblue.cn.tanzi.model.user.LoginResultModel;
import ahblue.cn.tanzi.ui.base.BaseActivity;
import ahblue.cn.tanzi.util.CodeCountTimer;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 更换手机号
 * Created by fengdongfei on 2018/11/9.
 */

public class UpdatePhoneActivity extends BaseActivity {

    @BindView(R.id.etname)
    EditText etname;
    @BindView(R.id.etpwd)
    EditText etpwd;
    @BindView(R.id.tvcodeget)
    TextView tvcodeget;
    private CodeCountTimer myCountTimer;

    public static void into(Activity activity) {
        activity.startActivity(new Intent(activity, UpdatePhoneActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_phone);
        ButterKnife.bind(this);
        myCountTimer = new CodeCountTimer(this, tvcodeget);
    }

    private void login() {
        String name = etname.getText().toString();
        String pwd = etpwd.getText().toString();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(UpdatePhoneActivity.this, "请输入您要更换的手机号", Toast.LENGTH_LONG).show();
        } else if (TextUtils.isEmpty(pwd)) {
            Toast.makeText(UpdatePhoneActivity.this, getResources().getString(R.string.no_code_input), Toast.LENGTH_LONG).show();
        } else {
            LoginResultModel.update(this, name, pwd, new LoginResultModel.LoginStatus() {
                @Override
                public void conver(LoginResultModel logmodel) {
                    if (null != logmodel) {
                        finish();
                    }
                }
            });
        }
    }

    @OnClick({R.id.tvlogin,  R.id.btn_back, R.id.tvcodeget})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvlogin:
                login();
                break;
            case R.id.btn_back:
                finish();
                break;
            case R.id.tvcodeget:
                etpwd.requestFocus();
                String name = etname.getText().toString();
                if (TextUtils.isEmpty(name)) {
                    Toast.makeText(UpdatePhoneActivity.this, "请输入手机号", Toast.LENGTH_LONG).show();
                } else{
                    CodeModel.updatePhone(this, etname.getText().toString(), new CodeModel.CodeListener() {
                        @Override
                        public void conver() {
                            myCountTimer.start();
                        }
                    });
                }
                break;
        }
    }
}
